import * as demo from '../../services/demo/index';
import pathToRegexp from 'path-to-regexp';
import {openNotice} from '../../utils'
export default {
  namespace: 'dome',
  state: {
    list: [], //
    loading:false,
    total: 0,
    filterData:{},
    pageParam: { //表格所需参数
      pageNo: 1,
      pageSize: 10,
      keyword: '',
      value:'',
      key:''
    }
  },
  reducers: {
    save(state, {payload}) {
      return {...state, ...payload};
    }
  },
  effects: {
    *fetch({payload}, { call, put, select}) {
      yield put({type: 'save', payload: {...payload}});
      const dome = yield select(function (state) {return state.dome});
      const data= yield call(demo.fetch,dome);
      yield put({
        type: 'save',
        payload: {
          list: data.data,
          total: parseInt(data.iTotalDisplayRecords, 10),
        },
      });
    },
    *del({ payload: id }, { call, put }) {
      let data= yield call(demo.del,id);
      if(data.status==200){
        openNotice('success', '提示', "删除成功");
        yield put({ type: 'reload' });
      }
    },
    *reload(action, { put, select }) {
      yield put({ type: 'fetch', payload: {pageParam: {pageNo: 1, pageSize: 10, keyword: ''}}});
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      //当路由发生变化，会触发这个
      return history.listen(({ pathname, query }) => {
        const match = pathToRegexp('/dome/:MenuId').exec(pathname);
        if (match) { // 初始化参数
          dispatch({ type: 'fetch', payload: {pageParam: {pageNo: 1, pageSize: 10,value:'', key:'',keyword:''}}});
        }});
    },
  },
};
